import clsx from "clsx";
import FieldComponent from "../../FieldComponent";
import { useForm } from "../../../framework/common/context/metadata-context";
import { diffStyle } from "../../../framework/common/utils/diffStyle";

/*
cell tplData.form.form_meta_data（表单设计器） 和  tplData.form.data_define（业务对象定义） 合并后的字段属性信息
isSubTableField true：子表字段 null：主表字段
codePath 子表拼接的对应value信息，路径为 tplData.form.form_data.children[子表code][行索引]
groupCode 字段所在分组code（子表才有）
rowIndex 字段所在的行索引（子表才有）
*/

export default function ({ hasSid, isSummary, row, cellIndex, cell, codePath, rowIndex, groupCode, isVirtualTable}) {
  const form = useForm();
  let isPrint=(form.mode==="Print")
  //行内容相同合并
  let rowSpan = null;
  if(isVirtualTable !== true){//isVirtualTable==true虚拟dom，导出excel时，不做行内容相同合并
    if (cell.mergedCell?.[rowIndex] === true) {
      return null;
    }
    if (cell.lineSpliceInfo?.[rowIndex] > 1) {
      rowSpan = cell.lineSpliceInfo[rowIndex];
    }
  }

  let fieldComponent = null;

  //生成配置的样式
  let fieldContentStyle = cell?.designProps?.styleData?.fieldContentStyle; //字段value样式
  let fieldValueClsName = diffStyle(fieldContentStyle, "fieldContentStyle"); //根据配置得到value的className
  if (!fieldValueClsName) {
    fieldValueClsName = form.fieldValueClsName; //没有自定义样式，就用全局的
  }

  //样式规则
  let valueStyle = cell?.contentStyles?.[rowIndex] || {};

  if (cell.code === "table_sid") {
    //如果cell类型是序号
    if (isSummary === true) {
      fieldComponent = null;
    } else {
      fieldComponent = rowIndex + 1;
    }
  } else if (isSummary && cell.designProps.isTotal !== true) {
    fieldComponent = null;
  } else {
    if (cell.isHiddenRows?.[rowIndex] !== true) {
      fieldComponent = <FieldComponent isSummary={isSummary} isSubTableField={true} groupCode={groupCode} codePath={codePath} rowIndex={rowIndex} cell={cell}></FieldComponent>;
    }
  }
  let levelStyle = {};

  //如果有缩进
  if (row.rowLevel > 0) {
    if (hasSid) {
      //序号列不缩进
      if (cellIndex === 1) levelStyle.paddingLeft = row.rowLevel * 26 + "px";
    } else {
      if (cellIndex === 0) levelStyle.paddingLeft = row.rowLevel * 26 + "px";
    }
  }
  return (
    <td rowSpan={rowSpan} data-code={cell?.code} data-type={cell?.type} className={clsx("col-cell", fieldValueClsName)} style={{ minWidth: isPrint!=true&&(cell.style?.width+"px"), ...valueStyle, ...levelStyle }}>
      {fieldComponent}
    </td>
  );
}
